package Login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action 
{
	public ActionForward execute( 	ActionMapping mapping, ActionForm form,	HttpServletRequest request,
									HttpServletResponse response) throws Exception
	{
		// create a new LoginBean with valid users in it
				LoginBean lb = new LoginBean();
				
				// check to see if this user/password combination are valid
				if(lb.validateUser(((LoginForm)form).getUserName(),((LoginForm)form).getPassword()))
				{
					request.setAttribute("userName",((LoginForm)form).getUserName()); //para el admin.jsp una vez q se loggea
					return (mapping.findForward("exito")); //actionForward con success
					//el mapeo se setea en el struts-config.xml
				}
				else	// username/password not validated
				{
					// create ActionError and save in the request
					ActionErrors errors = new ActionErrors();
					ActionError error = new ActionError("error.login.invalid");
					errors.add("login",error);
					saveErrors(request,errors); //guardo el actionErrors en el objeto request
					
					return (mapping.findForward("falla"));
				}
	}	
}